﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Wwtfly.Extensions
{

    /// <summary>
    /// 请求对象 扩展
    /// </summary>
   public static class RequestMyExtension
    {
        /// <summary>
        /// 获取通过传入的参数Url路径
        /// 没有参数,Referer中也没有,返回默认的
        /// </summary>
        /// <param name="requestQueryName">Url参数名称</param>
        /// <returns></returns>
       public static string GetRequestReturnUrl(this HttpRequest request, string requestQueryName, string defaultReturnUrl)
        {
            string url = request.QueryString[requestQueryName];
            if (string.IsNullOrEmpty(url))
            {
                if (!string.IsNullOrEmpty(request.Headers["Referer"]))
                {
                    url = request.Headers["Referer"];
                }
                else
                {
                    url = defaultReturnUrl;
                }
            }

            return url;
        }

        
       /// <summary>
       /// 获取远程用户IP地址
       /// (代理IP会造假的)
       /// </summary>
       /// <returns></returns>
       public static string GetRemoteIpAddress(this HttpRequestBase request)
       {
           var ipAddress = String.Empty;
           ipAddress = request.Headers["X-Forwarded-For"];
           if (string.IsNullOrEmpty(ipAddress))
           {
               ipAddress = request.UserHostAddress;
           }
           return ipAddress;
       }
    }
}
